﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.MefExtensions;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.ServiceLocation;
using System.Windows;
using Microsoft.Practices.Prism.Logging;
using Microsoft.Practices.Prism.Modularity;
using Upreader.Engine;
using Upreader.Application.Default;
using Upreader.Infrastructure.ProtoBuf;

namespace Upreader.Presentation.Client
{
    public class UpreaderBootstrapper : MefBootstrapper  
    {
        protected override System.Windows.DependencyObject CreateShell()
        {
            return Container.GetExportedValue<MainWindow>();
        }

        protected override void InitializeShell()
        {
            System.Windows.Application.Current.MainWindow = (Window)Shell;
            System.Windows.Application.Current.MainWindow.Show(); 
        }

        protected override void ConfigureAggregateCatalog()
        {            
            AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(UpreaderBootstrapper).Assembly));
        }

        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();

            var database = new ProtoBufDatabase("store.upreader");
            database.Open();

            var service = new ApplicationService(() => new ProtoBufDomainContext(database.GetDataContext()));
            var downloadEngine = new DownloadEngine(service);

            Container.ComposeExportedValue<DownloadEngine>(downloadEngine);
        }
    }
}
